#include"Shm.hpp"
#include"name_pipe.hpp"

int main()
{
    //获取key值
    int key = GetShmKeyorDie(); 
    cout << "key: " << ToHex(key) << endl;

    //1.创建共享内存
    int shmid = CreatShm(key);
    cout << "shmid: " << shmid << endl;

    //2.进程连接到共享内存,构建映射
    char* addr = (char*)AttShm(shmid);

    //3.进程间通信 
    //共享内存不提供进程间协同的任何机制(同步、互斥机制,用来保护共享资源)
    Fifo fifo(Path); //管道内部自带同步、互斥机制
    Sysn sn; 
    sn.OpenReadorDie(); //以只读的方式打开管道
    for(; ;)
    {
        if(sn.Wait()) //等待客户端写完
        {
            cout << "shm content: " << addr << endl; 
            sleep(1);
        }
        else break; 
    }

    //4.断开共享内存的连接,解除映射
    DetachShm(addr);

    //5.删除共享内存
    DelShm(shmid);

    return 0;
}